<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
	<title>Month View</title>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
	<script type="text/javascript" src="../../include-all.js"></script>
    <script type="text/javascript" src="common-types.js"></script>
	<script type="text/javascript">
		var view = new dorado.widget.View({ layout: "Anchor"});

		var monthview;

        function fixDate(d, check) { // force d to be on check's YMD, for daylight savings purposes
            if (+d) { // prevent infinite looping on invalid dates
                while (d.getDate() != check.getDate()) {
                    d.setTime(+d + (d < check ? 1 : -1) * HOUR_MS);
                }
            }
        }

        function parseISO8601(s, ignoreTimezone) { // ignoreTimezone defaults to false
            // derived from http://delete.me.uk/2005/03/iso8601.html
            var m = s.match(/^([0-9]{4})(-([0-9]{2})(-([0-9]{2})([T ]([0-9]{2}):([0-9]{2})(:([0-9]{2})(\.([0-9]+))?)?(Z|(([-+])([0-9]{2})(:?([0-9]{2}))?))?)?)?)?$/);
            if (!m) {
                return null;
            }
            var date = new Date(m[1], 0, 1);
            if (ignoreTimezone || !m[13]) {
                var check = new Date(m[1], 0, 1, 9, 0);
                if (m[3]) {
                    date.setMonth(m[3] - 1);
                    check.setMonth(m[3] - 1);
                }
                if (m[5]) {
                    date.setDate(m[5]);
                    check.setDate(m[5]);
                }
                fixDate(date, check);
                if (m[7]) {
                    date.setHours(m[7]);
                }
                if (m[8]) {
                    date.setMinutes(m[8]);
                }
                if (m[10]) {
                    date.setSeconds(m[10]);
                }
                if (m[12]) {
                    date.setMilliseconds(Number("0." + m[12]) * 1000);
                }
                fixDate(date, check);
            }else{
                date.setUTCFullYear(
                    m[1],
                    m[3] ? m[3] - 1 : 0,
                    m[5] || 1
                );
                date.setUTCHours(
                    m[7] || 0,
                    m[8] || 0,
                    m[10] || 0,
                    m[12] ? Number("0." + m[12]) * 1000 : 0
                );
                if (m[14]) {
                    var offset = Number(m[16]) * 60 + (m[18] ? Number(m[18]) : 0);
                    offset *= m[15] == '-' ? 1 : -1;
                    date = new Date(+date + (offset * 60 * 1000));
                }
            }
            return date;
        }

		$(document).ready(function() {
            $.ajax({
                url:'sample.xml',
                type: 'get',
                dataType: 'xml',
                timeout: 100000,
                error: function(xml) {
                    alert('Error loading XML document' + xml);
                },
                success: function(xml) {
                    var item, items = [];

                    $(xml).find("entry").each(function(i, entry) {
                        var allDay = $(entry).find("when").attr("startTime").indexOf('T') == -1;
                        item = new dorado.widget.CalEvent({
                            title : $(entry).find("title").text(),
                            content : $(entry).find("content").text(),
                            startTime: new XDate(parseISO8601($(entry).find("when").attr("startTime"), true)),
                            endTime: new XDate(parseISO8601($(entry).find("when").attr("endTime"), true)),
                            allDay: allDay
                        });
                        items.push(item);
                    });
                    var json = "";
                    items.forEach(function(item, index){
                        json += "{";
                        json += "\ttitle:" + item.title + ",";
                        json += "\tstartTime:" + item.startTime + ",";
                        json += "\tendTime:" + item.endTime + ",";
                        json += "\tallDay:" + item.allDay;
                        json += "},";
                    });
                    //console.log(json);
                    monthview = new dorado.widget.MonthView({
                        eventSource: new dorado.widget.ArrayEventSource({
                            array: items
                        }),
                        width: "100%",
                        height: "100%"
                    });
                    view.addChild(monthview);
                    view.render(document.body);
                }
            });
		});
	</script>
</head>
<body style="margin: 0; padding: 0;">
</body>
</html>